/**
 * Created by Administrator on 2017/6/27.
 */
define(['jquery', 'vue'], function ($, Vue) {
    var searchValue = "";
    var type = 0;
    var currentPage = 1;
    var keyword = "";
    var order = "id";
    var sort = "desc";
    var city_searchValue = "";



    // var _self;
    mui.init({
        pullRefresh: {
            container: '#refreshContainer',
            down: {
                height: 1,//可选,默认50.触发下拉刷新拖动距离,
                auto: false,//可选,默认false.自动下拉刷新一次
                contentdown: "正在刷新...",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
                contentover: "正在刷新...",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
                contentrefresh: "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                callback: pulldownRefresh//必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            },
            up: {
                contentrefresh: '正在加载...',
                contentnomore: '没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: pullupRefresh
            }
        }
    });
    var vue = new Vue({
        el: '#data_ul',
        data: {
            items: []
        },
        methods: {
            detailbtn: function (id) {
                window.location.href= goods_url+"/store_id/"+id;
            },
            notCollect:function (id) {
                $.post(del_collect_url, {store_id: id},
                    function (result) {
                        if (result.code === 1) {
                            location.reload();
                            mui.toast("店铺已取消收藏");
                        } else {
                            mui.toast(result.msg);
                        }
                    });
            }
        }
    });
    getData("up");
    function getData(flag) {
        var data = {
            'order': order,
            "sort": sort,
            'city_searchValue': city_searchValue,
            "keyword": keyword,
            'page': currentPage
        };
        $.get(list_url, data,
            function (result) {
                if (result.code === 1) {
                    switch (flag) {
                        case "up":
                            if(result.data.length>0)
                            {
                                vue.items.push(result.data);
                                currentPage++;
                                $("#tip-icon").hide();
                                mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                            }
                            else {
                                if(currentPage===1){
                                    $("#tip-icon").show();
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                                }else{
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                                }
                            }
                            break;
                        case "down":
                            mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
                            break;
                    }
                }
            });
    }

    /**
     * 上拉加载具体业务实现
     */
    function pullupRefresh() {
        getData("up");
    }

    /**
     * 下拉刷新具体业务实现
     */
    function pulldownRefresh() {
        getData("down");
    }
});